package com.study.concurrentprogramming.threadPool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description : thread pool test
 * @Version : V1.0.0
 * @Date : 2022/1/12 9:15
 */
public class ThreadPoolTest {

    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
            0,
            30,
            60L,
            TimeUnit.SECONDS,
            new SynchronousQueue<>(),
            new ThreadFactoryBuilder().setNameFormat("person-faceSet-sync-thread-%d").build(),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static void main(String[] args) {
        List<Future<String>> futures = new ArrayList<>();
        MyCallable myCallable = new MyCallable();

        for (int i = 0; i < 30; i++) {
            Future<String> submit = executor.submit(myCallable);
            futures.add(submit);
        }

        for (Future<String> future : futures) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("task exe end");
    }
}
